package com.prs.crm.dao.cdxh.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.cdxh.ProvideRegimeDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.cdxh.production.MachineClass;
import com.prs.crm.domain.cdxh.production.ProvideRegime;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2010-12-3 上午10:00:53 简单说明
 */

public class ProvideRegimeDaoImpl extends BaseDaoImpl<ProvideRegime> implements
		ProvideRegimeDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public ProvideRegime getProvideRegime(final String productStyles,
			final String recodeStyle, final String classS,
			final String machiness) {
		return (ProvideRegime) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(ProvideRegime.class);
						criteria.add(Restrictions.eq("productStyle",
								productStyles));
						criteria.add(Restrictions
								.eq("recodeStyle", recodeStyle));
						criteria.add(Restrictions.eq("machineNums", machiness));
						criteria.add(Restrictions.eq("classNums", classS));
						List<ProvideRegime> result = criteria.list();
						if (result.size() == 0) {
							return null;
						} else {
							return result.get(0);
						}
					}
				});
	}

	@Override
	public void setProvideRegime(ProvideRegime provideRegime) {
		this.getHibernateTemplate().saveOrUpdate(provideRegime);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public ProvideRegime getReplaceProvideRegime(final String productStyle,
			final String machineNums,final String classNums,final String recodeStyle) {
		return (ProvideRegime)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(ProvideRegime.class);
				        	criteria.add(Restrictions.eq("productStyle", productStyle));	
				        	criteria.add(Restrictions.eq("machineNums", machineNums));
				        	criteria.add(Restrictions.eq("classNums", classNums));
				        	criteria.add(Restrictions.eq("recodeStyle", recodeStyle));
				        	List<ProvideRegime> result = criteria.list();
				        	
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}
}
